/********************************************************************************
 * ConfigurationAction.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Sep 8, 2008 10:37:42 AM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/

package com.printeverywhere.action;

import java.util.Map;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionContext;
import com.printeverywhere.delegator.ConfigurationServiceDelegator;
import com.printeverywhere.delegator.LetterServiceDelegator;
import com.printeverywhere.dto.Configuration;
import com.printeverywhere.util.AdminConstants;

// TODO: Auto-generated Javadoc
/**
 * The Class ConfigurationAction.
 */
public class ConfigurationAction  extends AdminSystemSetup {

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(ConfigurationAction.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The configuration. */
	private Configuration configuration;

	/**
	 * Gets the configuration.
	 *
	 * @return the configuration
	 */
	public Configuration getConfiguration() {
		return configuration;
	}

	/**
	 * Sets the configuration.
	 *
	 * @param configuration the new configuration
	 */
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}

		this.configuration = ConfigurationServiceDelegator.getConfiguration();

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Update configuration.
	 *
	 * @return the string
	 *
	 * @throws Exception the exception
	 */
	public String updateConfiguration() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("updateConfiguration() - start"); //$NON-NLS-1$
		}
		try{
		if(checkFirstCharacter(configuration.getDefaultMetaTitle(), AdminConstants.DEFAULTMETATITLE)){
			return INPUT;
		}
		if(checkFirstCharacter(configuration.getDefaultMetaDescription(), AdminConstants.DEFAULTMETADESCRIPTION)){
			return INPUT;
		}
		if(checkFirstCharacter(configuration.getDefaultMetaKeywords(), AdminConstants.DEFAULTMETAKEYWORDS)){
			return INPUT;
		}
		addActionMessage("Configuration updated successfully.");
		ConfigurationServiceDelegator.updateConfiguration(this.configuration);
		}catch (Exception e) {
			addActionError("Configuration not updated.");
		}
		if (logger.isDebugEnabled()) {
			logger.debug("updateConfiguration() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	private boolean checkFirstCharacter(String meta, String fieldName){
		if(meta.startsWith(AdminConstants.COLON)){
			addActionError("First Character can not be colon for field ".intern()+fieldName+AdminConstants.DOT);
			return AdminConstants.TRUE;
		}
		if(meta.startsWith(AdminConstants.UNDERSCORE)){
			addActionError("First Character can not be underscore for field ".intern()+fieldName+AdminConstants.DOT);
			return AdminConstants.TRUE;
		}
		if(meta.startsWith(AdminConstants.BLANK)){
			addActionError("First Character can not be space for field ".intern()+fieldName+AdminConstants.DOT);
			return AdminConstants.TRUE;
		}
		return AdminConstants.FALSE;
	}

	@SuppressWarnings("unused")
	private void prep() {
		if (logger.isDebugEnabled()) {
			logger.debug("prep() - start"); //$NON-NLS-1$
		}

		if (logger.isDebugEnabled()) {
			logger.debug("prep() - end"); //$NON-NLS-1$
		}
	}
}